package com.bah.tract.client.ui;

import java.security.GeneralSecurityException;
import java.util.prefs.Preferences;

import javax.inject.Inject;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.bah.tract.client.Constants;
import com.bah.tract.client.ErrorHandler;
import com.bah.tract.security.PasswordEncryptor;

public class JiraPreferencePanel extends PreferencePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPasswordField jiraPassword;
	private JTextField jiraUser;
	private final Preferences mPreferences;
	private final PasswordEncryptor mPasswordEncryptor;

	@Inject
	public JiraPreferencePanel(final Preferences preferences,
			final PasswordEncryptor passwordEncryptor) {
		mPreferences = preferences;
		mPasswordEncryptor = passwordEncryptor;
		initComponents();
	}

	private void initComponents() {

		JLabel userLbl = new javax.swing.JLabel();
		JLabel passwordLbl = new javax.swing.JLabel();
		jiraPassword = new javax.swing.JPasswordField();
		jiraUser = new javax.swing.JTextField();

		setName("jPanel1"); // NOI18N

		userLbl.setText("User ID"); // NOI18N
		userLbl.setName("jLabel4"); // NOI18N

		passwordLbl.setText("Password"); // NOI18N
		passwordLbl.setName("jLabel5"); // NOI18N
		final String encryptedPass = mPreferences.get(Constants.JIRA_PASS_PREF,
				null);
		String password;
		if (encryptedPass == null) {
			password = "password";
		} else {
			try {
				password = mPasswordEncryptor.decrypt(encryptedPass);
			} catch (final GeneralSecurityException e1) {
				password = "password";
			}
		}

		jiraPassword.setText(password); // NOI18N
		jiraPassword.setName("jPasswordField1"); // NOI18N
		final String jiraUserId = mPreferences.get(Constants.JIRA_ID_PREF,
				"        ");
		jiraUser.setText(jiraUserId); // NOI18N

		jiraUser.setName("jTextField1"); // NOI18N

		final javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				this);
		setLayout(jPanel1Layout);
		setBorder(BorderFactory.createTitledBorder("Jira"));
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																passwordLbl)
														.addComponent(userLbl))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jiraUser,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jiraPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(226, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(19, 19, 19)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(userLbl)
														.addComponent(
																jiraUser,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																passwordLbl)
														.addComponent(
																jiraPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

	}

	@Override
	public void savePreferences() {
		final String password = new String(jiraPassword.getPassword());
		String encryptedPassword = null;
		try {
			encryptedPassword = mPasswordEncryptor.encrypt(password);
		} catch (final GeneralSecurityException e1) {
			ErrorHandler.handleError(e1);
		}
		mPreferences.put(Constants.JIRA_PASS_PREF, encryptedPassword);
		mPreferences.put(Constants.JIRA_ID_PREF, jiraUser.getText().trim());
	}

}
